<template>
	<view class="communityListPage">
		<view class="searchContent">
			<u-input v-model="keyWord" shape="circle" inputAlign="center" placeholder="搜索" @confirm="doSearch">
				<view slot="suffix" class="iconClass" @click="doSearch">
					<u-icon name="search" color="#004DA1" size="52rpx"></u-icon>
				</view>
			</u-input>
		</view>
		<view class="tabAndFilter">
			<view class="tabContent">
				<view @click="changeTab(item.id)" v-for="(item,index) in tabs" :key="index" class="tabItem"
					:class="activeTab===item.id?'isActive':''">{{item.columnName}}</view>
			</view>
			<view class="filterContent">
				<u--image src="/static/community/2.png" shape="square" mode="aspectFill" width="30rpx" height="30rpx"
					bgColor="transparent" @click="showFilterPanel=!showFilterPanel"></u--image>
				<view class="filterList" v-if="showFilterPanel">
					<view class="item" v-for="(item,index) in filters" :key="index" @click="changeFilter(item)">
						{{item.name}}
						<u--image v-if="activeFilter===item.id" src="/static/community/3.png" shape="square"
							mode="aspectFill" width="28rpx" height="18rpx" bgColor="transparent"></u--image>
					</view>
				</view>
			</view>
		</view>
		<view class="listContent">
			<u-empty mode="list" icon="http://cdn.uviewui.com/uview/empty/list.png" v-if="!list2.length && !isLoading" text="没有更多了"></u-empty>
			<view v-for="(item,index) in list2" :key="index" class="list" @click="goDetail(item.id)">
				<view class="itemTop">
					<view class="topLeft">
						<view class="title">{{item.name}}</view>
						<view class="talk">
							<view v-for="(talk,num) in item.topicName?item.topicName.split('，'):[]" :key="num" class="talkItem">
								#{{talk}}
							</view>
						</view>
					</view>
					<view class="topRight">
						<u--image :src="item.images?(baseUrl+'/sys/common/static/'+item.images.split(',')[0]):''" shape="square" mode="aspectFill" width="208rpx" height="150rpx"
							bgColor="transparent" radius="8rpx"></u--image>
					</view>
				</view>
				<view class="itemBottom">
					<view class="operate">
						<view class="item">
							<u--image src="/static/community/4.png" shape="square" mode="aspectFill" width="30rpx"
								height="24rpx"></u--image>
							<view class="count">{{item.readingVolume||0}}</view>
						</view>
						<view class="item">
							<u--image src="/static/community/5.png" shape="square" mode="aspectFill" width="26rpx"
								height="26rpx"></u--image>
							<view class="count">{{item.replyCount||0}}</view>
						</view>
					</view>
					<view class="createtime">
						{{item.createTime}}
					</view>
				</view>
			</view>
		</view>
		<u-loadmore  v-if="list2.length" :status="isFinish?'nomore':'loading'" loading-text="正在加载" loadmore-text="加载更多"
			nomore-text="没有更多了" color="#ccc"/>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				iconClass:{
					color: "#004DA1",
					fontSize:"52rpx",
					borderLeft:"1rpx solid #D0D0D0",
					padding:"0 0 0 38rpx"
				},
				keyWord:"",
				tabs: [{
					id: "",
					columnName: "推荐"
				}],
				activeTab: "",
				filters: [{
					id: "1",
					name: "发布时间"
				}, {
					id: "2",
					name: "阅读量"
				}, {
					id: "3",
					name: "评论数"
				}],
				activeFilter: "1",
				showFilterPanel: false,
				list2: [],
				pageNo: 1,
				pageSize: 10,
				total: 0,
				isFinish: false,
				isLoading: false,
				userId:"",
				baseUrl: this.$api.baseUrl,
			}
		},
		onLoad() {
			uni.getStorage({
				key: "userInfo",
				success: (userInfo) => {
					this.userId = userInfo.data.id;
					this.initData();
				}
			})
			Promise.all([
				this.$request.get(this.$api.columnsList,{
					pageNo:"1",
					pageSize:"100"
				})
			]).then(res=>{
				this.tabs = this.tabs.concat(res[0].result.records)
				uni.hideLoading()
			})
		},
		onReachBottom() {
			if (!this.isLoading && !this.isFinish) {
				this.pageNo++;
				this.initData();
			}
		},
		onPullDownRefresh() {
			this.doSearch()
		},
		methods: {
			doSearch() {
				this.isFinish = false;
				if (!this.isLoading) {
					this.pageNo = 1;
					this.list2 = [];
					this.initData(true);
				}
			},
			goDetail(id) {
				uni.navigateTo({
					url: "/pages/postDetail/postDetail?id=" + id
				})
			},
			refreshData(){
				this.pageNo = 1;
				this.list2 = [];
				this.initData();
			},
			initData(isRefresh){
				this.isLoading = true;
				this.$request.get(this.$api.posts,{
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					userId:this.userId,
					type:this.activeFilter,
					columnId:this.activeTab,
					name:this.keyWord
				}).then(res=>{
					if (isRefresh) {
						uni.stopPullDownRefresh()
					}
					this.isLoading = false;
					this.hasData = res.result.total;
					this.list2 = this.list2.concat(res.result.records)
					console.log(this.list2)
					this.total = res.result.total;
					if (this.pageNo * this.pageSize < this.total) {
						this.isFinish = false
					} else {
						this.isFinish = true
					}
					uni.hideLoading()
				})
			},
			changeTab(id){
				this.activeTab = id;
				this.refreshData();
			},
			changeFilter(item) {
				this.activeFilter = item.id;
				this.showFilterPanel = false;
				this.refreshData();
			},
		}
	}
</script>

<style lang="scss">
	.communityListPage {
		.searchContent {
			padding: 0rpx 40rpx 26rpx;
			background-color: #FAFAFA;
			
			.iconClass {
				border-left: 1rpx solid #D0D0D0;
				padding: 0 0 0 38rpx;
			}
		}
		.communityInfoSwiper {
			padding: 0 40rpx;
			width: calc(100% - 80rpx);
		}

		.tabAndFilter {
			display: flex;
			justify-content: flex-start;

			.tabContent {
				display: flex;
				flex-wrap: nowrap;
				justify-content: flex-start;
				padding: 0 40rpx;
				width: calc(100vw - 160rpx);
				overflow: auto;

				.tabItem {
					background: #F4F4F4;
					border-radius: 4px 4px 4px 4px;
					color: #333333FF;
					font-size: 26rpx;
					height: 62rpx;
					padding: 0 20rpx;
					line-height: 62rpx;
					margin-right: 20rpx;
					flex-shrink: 0;
				}

				.isActive {
					color: #004DA1FF;
					background-color: #E5EDFFFF;
				}
			}

			.filterContent {
				width: 80rpx;
				display: flex;
				justify-content: center;
				align-items: center;
				position: relative;

				.filterList {
					display: flex;
					flex-wrap: wrap;
					flex-direction: column;
					position: absolute;
					background: #fff;
					top: 60rpx;
					right: 40rpx;
					width: 240rpx;
					background: #FFFFFF;
					box-shadow: 0px 3px 6px 1px rgba(0, 0, 0, 0.16);
					border-radius: 10px 10px 10px 10px;
					z-index: 9;

					.item {
						font-family: PingFang SC-Regular, PingFang SC;
						font-weight: 400;
						color: #666666;
						border-bottom: 1px solid #EEEEEEFF;
						height: 100rpx;
						line-height: 100rpx;
						padding: 0 30rpx;
						display: flex;
						flex-wrap: nowrap;
						justify-content: space-between;
						align-items: center;
					}
				}
			}
		}

		.listContent {
			display: flex;
			flex-direction: column;
			padding: 20rpx 40rpx;

			.list {
				display: flex;
				flex-direction: column;
				padding: 30rpx 20rpx 24rpx;
				background: #FFFFFF;
				box-shadow: inset 0px 0px 0px 1rpx rgba(230, 230, 230, .5);
				border-radius: 10px 10px 10px 10px;
				margin-bottom: 20rpx;

				.itemTop {
					display: flex;
					flex-wrap: nowrap;
					justify-content: flex-start;
					padding-bottom: 20rpx;

					.topLeft {
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						width: 100%;

						.title {
							font-family: PingFang SC-Bold, PingFang SC;
							font-weight: bold;
							color: #333333;
							font-size: 28rpx;
							letter-spacing: 1rpx;
							overflow: hidden;
							text-overflow: ellipsis;
							display: -webkit-box;
							-webkit-line-clamp: 3;
							-webkit-box-orient: vertical;
							padding-right: 20rpx;
						}

						.talk {
							font-family: PingFang SC-Regular, PingFang SC;
							font-weight: 400;
							color: #004DA1;
							font-size: 26rpx;
							display: flex;
							flex-wrap: nowrap;
							overflow: auto;
							margin-right: 20rpx;

							.talkItem {
								flex-shrink: 0;
							}
						}
					}

					.topRight {
						flex-shrink: 0;
					}
				}

				.itemBottom {
					display: flex;
					flex-wrap: nowrap;
					justify-content: space-between;
					font-family: PingFang SC-Regular, PingFang SC;
					font-weight: 400;
					color: #999999;
					font-size: 26rpx;

					.operate {
						display: flex;
						flex-wrap: nowrap;

						.item {
							display: flex;
							flex-wrap: nowrap;
							align-items: center;
							padding-right: 40rpx;

							.count {
								padding-left: 10rpx;
							}
						}
					}
				}
			}
		}
	}
</style>
